<script>
function enrollPupil($item) {
	$item.removeClass().addClass('ui-state-disabled');
	$item.unbind('click').click(function() {
		unenrollPupil($(this));
	});
	var selectedroom = $('#selectedroom').val();
	var date = $("#date").val();
	$.getJSON('/~Marc/kivaou/ajax/roll_call', {
		'room': selectedroom,
		'pupil_id': $item.attr('pupil_id'),
		'date': date,
		'status':1
	}, function(result) {
		console.log(result);
	});
}
function unenrollPupil($item) {
	$item.removeClass().addClass('ui-state-default');
	$item.unbind('click').click(function() {
		enrollPupil($(this));
	});
	var selectedroom = $('#selectedroom').val();
	var date = $("#date").val();
	$.getJSON('/~Marc/kivaou/ajax/roll_call', {
		'room': selectedroom,
		'pupil_id': $item.attr('pupil_id'),
		'date': date,
		'status':0
	}, function(result) {
		console.log(result);
	});
}

function getEntries() {
	var selectedroom = $('#selectedroom').val();
	var date = $("#date").val();
	$.getJSON('/~Marc/kivaou/ajax/get_entries', {
		'room': selectedroom,
		'date': date
	}, function(result) {
		var contentHtml = "";
		$(result).each(

		function(index, item) {
			if(item.roll_call == 0){
				contentHtml += "<li pupil_class=\"" + item.pupilclass + "\" pupil_id=\"" + item.id + "\" class=\"ui-state-default\">";
			}else{
				contentHtml += "<li pupil_class=\"" + item.pupilclass + "\" pupil_id=\"" + item.id + "\" class=\"ui-state-disabled\">";				
			}
			contentHtml += item.surname;
			contentHtml += " ";
			contentHtml += item.name;
			contentHtml += " ( ";
			contentHtml += item.pupilclass;
			contentHtml += " )";
			contentHtml += "</li>";
		});
		$("#entriesdata").html(contentHtml);
		var $entriesdata = $("#entriesdata");
		$("li", $entriesdata).click(function() {
			enrollPupil($(this));
		});
	});
}

$("#date").datepicker({
	beforeShowDay: $.datepicker.noWeekends
}).on('changeDate', function(ev) {
	getEntries();
})
$('#selectedroom').change(function() {
	getEntries();
});
</script>